﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DingHeng.TransportationManagement.Business;
using DingHeng.TransportationManagement.Entity.Constant;
using DingHeng.TransportationManagement.Entity;
using DingHeng.TransportationManagement.WebCommon;
using DingHeng.TransportationManagement.WebCommon.BaseUI;
using DingHeng.TransportationManagement.WebCommon.Extension;

namespace DingHeng.TransportationManagement.WebSite.Biz
{
    public partial class TransportationFormSubmit : BasePage
    {
        private DingHeng.TransportationManagement.Entity.TransportationForm TransportationForm
        {
            get
            {
                if (ViewState["TransportationFormInstance"] == null)
                {
                    ViewState["TransportationFormInstance"] = new DingHeng.TransportationManagement.Entity.TransportationForm();
                }
                return ViewState["TransportationFormInstance"] as DingHeng.TransportationManagement.Entity.TransportationForm;
            }
            set
            {
                ViewState["TransportationFormInstance"] = value;
            }
        }
        private Guid? TransportationFormID
        {
            get
            {
                Guid id;
                if (string.IsNullOrEmpty(Request.QueryString.Get("TransportationFormID")) || Guid.TryParse(Request.QueryString.Get("TransportationFormID"), out id) == false)
                {
                    return null;
                }
                else
                {
                    return id;
                }
            }
        }
        private void initPage()
        {

            List<User> userList = new UserBiz().LoadActiveUsers();
            this.ddlLoadConfirmUser.BindData<User>(userList);
            //this.ddlLoadShiftUser.BindData<User>(userList);
            this.ddlUnloadConfirmUser.BindData<User>(userList);
            //this.ddlUnloadShiftUser.BindData<User>(userList);

            List<DictionaryData> shiftTypeList = new DictionaryDataBiz().GetByType(DictionaryDataType.ShiftType.ToString());
            this.ddlLoadShiftType.BindData<DictionaryData>(shiftTypeList);
            this.ddlUnloadShiftType.BindData<DictionaryData>(shiftTypeList);
            List<DictionaryData> statusList = new DictionaryDataBiz().GetByType(DictionaryDataType.Status.ToString());
            this.ddlStatus.BindData<DictionaryData>(statusList);

            VehicleDispatchFormInfo vehicleDispatchFormInfo = new VehicleDispatchFormBiz().LoadByInfoID(TransportationForm.VehicleDispatchID.Value);
            this.lblDriver.Text = vehicleDispatchFormInfo.Driver;
            this.lblLicensePlateNo.Text = vehicleDispatchFormInfo.LicensePlateNo;
            this.lblPrincipal.Text = vehicleDispatchFormInfo.PrincipalName;
            this.lblVehicleCode.Text = vehicleDispatchFormInfo.VehicleCode;
            this.lblVehicleDispatchNo.Text = vehicleDispatchFormInfo.Code;
        }
        private void loaded()
        {
            this.ddlStatus.SelectedValue(this.TransportationForm.Status.ToString());
            //this.ddlLoadShiftType.SelectedValue(this.TransportationForm.LoadShiftType.ToString());
            this.ddlLoadConfirmUser.SelectedValue(WebContext.Current.CurrentUser.ID.ToString());
            //this.ddlLoadShiftUser.SelectedValue(WebContext.Current.CurrentUser.ID.ToString());

            this.ddlUnloadConfirmUser.Visible = false;
            this.ddlUnloadShiftType.Visible = false;
            //this.ddlUnloadShiftUser.Visible = false;

            this.txtUnloadQuantity.Enabled = false;
            this.txtUnloadTatleQuantity.Enabled = false;
            this.txtUnloadVehicleQuantity.Enabled = false;
            this.btnSubmit.Visible = true;
        }
        private void arriving()
        {
            this.ddlStatus.SelectedValue(StringConstant.StatusArrived);
            //this.ddlLoadShiftType.SelectedValue(this.TransportationForm.LoadShiftType.ToString());
            //this.ddlLoadConfirmUser.SelectedValue(this.TransportationForm.LoadConfirmUser.ToString());
            //this.ddlLoadShiftUser.SelectedValue(this.TransportationForm.LoadShiftUser.ToString());

            this.ddlLoadShiftType.Enabled = false;
            this.ddlLoadConfirmUser.Enabled = false;
            //this.ddlLoadShiftUser.Enabled = false;
            this.ddlUnloadConfirmUser.Visible = false;
            this.ddlUnloadShiftType.Visible = false;
            //this.ddlUnloadShiftUser.Visible = false;
            this.btnArriving.Visible = true;
            this.txtUnloadQuantity.Enabled = false;
            this.txtUnloadTatleQuantity.Enabled = false;
            this.txtUnloadVehicleQuantity.Enabled = false;
            this.txtLoadQuantity.Enabled = false;
            this.txtLoadTatleQuantity.Enabled = false;
            this.txtLoadVehicleQuantity.Enabled = false;
            //this.ddlUnloadConfirmUser.SelectedValue(WebContext.Current.CurrentUser.ID.ToString());
            //this.ddlUnloadShiftUser.SelectedValue(WebContext.Current.CurrentUser.ID.ToString());
            //this.ddlUnloadShiftType.SelectedValue(new Common().ShiftTypeCalculate(DateTime.Now, out shiftDate));

        }
        private void unloaded()
        {
            this.btnUnloaded.Visible = true;
            this.ddlStatus.SelectedValue(StringConstant.StatusUnloaded);
            //this.ddlLoadShiftType.SelectedValue(this.TransportationForm.LoadShiftType.ToString());
            //this.ddlLoadConfirmUser.SelectedValue(this.TransportationForm.LoadConfirmUser.ToString());
            //this.ddlLoadShiftUser.SelectedValue(this.TransportationForm.LoadShiftUser.ToString());

            this.ddlLoadShiftType.Enabled = false;
            this.ddlLoadConfirmUser.Enabled = false;
            //this.ddlLoadShiftUser.Enabled = false;

            this.ddlUnloadConfirmUser.SelectedValue(WebContext.Current.CurrentUser.ID.ToString());
            //this.ddlUnloadShiftUser.SelectedValue(WebContext.Current.CurrentUser.ID.ToString());
            
            this.ddlUnloadShiftType.SelectedValue(WebContext.Current.CurrentUser.ShiftType);
            //this.TransportationForm.UnloadShiftDate = WebContext.Current.CurrentUser.ShiftDate;
            this.txtLoadQuantity.Enabled = false;
            this.txtLoadTatleQuantity.Enabled = false;
            this.txtLoadVehicleQuantity.Enabled = false;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!TransportationFormID.HasValue)
                {
                    return;
                }
                this.TransportationForm = new TransportationFormBiz().LoadById(TransportationFormID.Value);
                if (this.TransportationForm == null)
                {
                    return;
                }
                initPage();
                if (this.TransportationForm.Status == Guid.Parse(StringConstant.StatusLoaded))
                {
                    lblHomeTitle.Text = "车辆装货登记";
                    loaded();
                }
                if (this.TransportationForm.Status == Guid.Parse(StringConstant.StatusOnRoad))
                {
                    lblHomeTitle.Text = "车辆入厂登记";
                    arriving();
                }
                if (this.TransportationForm.Status == Guid.Parse(StringConstant.StatusArrived))
                {
                    lblHomeTitle.Text = "车辆卸货登记";
                    unloaded();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            decimal quantity = 0.0M;
            if (decimal.TryParse(this.txtLoadQuantity.Text, out quantity))
            {
                this.TransportationForm.LoadQuantity = quantity;
            }
            else
            {
                MessageBox("装车地净重量输入有误！");
            }
            if (decimal.TryParse(this.txtLoadTatleQuantity.Text, out quantity))
            {
                this.TransportationForm.LoadTotalQuantity = quantity;
            }
            else
            {
                MessageBox("装车地总重量输入有误！");
            }
            if (decimal.TryParse(this.txtLoadVehicleQuantity.Text, out quantity))
            {
                this.TransportationForm.LoadVehicleQuantity = quantity;
            }
            else
            {
                MessageBox("装车地皮重量输入有误！");
            }
            //this.TransportationForm.LoadArchiveNo = null;
            this.TransportationForm.LoadLocation = null;

            //this.TransportationForm.LoadConfirmUser = Guid.Parse(this.ddlLoadConfirmUser.SelectedValue);
            //this.TransportationForm.LoadShiftUser = WebContext.Current.CurrentUser.ID;
            this.TransportationForm.StartTime = DateTime.Now;
            this.TransportationForm.Status = Guid.Parse(StringConstant.StatusOnRoad);
            this.TransportationForm.UpdateBy = WebContext.Current.CurrentUser.Name;
            this.TransportationForm.UpdateDate = DateTime.Now;
            //DateTime shiftDate;
            //new Common().ShiftTypeCalculate(DateTime.Now, out shiftDate);

            //this.TransportationForm.LoadShiftDate = WebContext.Current.CurrentUser.ShiftDate;
            //this.TransportationForm.LoadShiftType = Guid.Parse(this.ddlLoadShiftType.SelectedValue);
            
            new TransportationFormBiz().Save(this.TransportationForm);
            MessageBox("装货登记成功");
            lockControls();

        }

        private void lockControls()
        {
            this.txtLoadQuantity.Enabled = false;
            this.txtUnloadQuantity.Enabled = false;
            this.ddlLoadConfirmUser.Enabled = false;
            this.ddlLoadShiftType.Enabled = false;
            //this.ddlLoadShiftUser.Enabled = false;
            this.ddlStatus.Enabled = false;
            this.ddlUnloadConfirmUser.Enabled = false;
            this.ddlUnloadShiftType.Enabled = false;
            //this.ddlUnloadShiftUser.Enabled = false;
            this.btnSubmit.Visible = false;
            this.btnArriving.Visible = false;
            this.btnUnloaded.Visible = false;
        }

        protected void btnArriving_Click(object sender, EventArgs e)
        {
            this.TransportationForm.Status=Guid.Parse(StringConstant.StatusArrived);
            this.TransportationForm.ArriveTime = DateTime.Now;
            TimeSpan duration=DateTime.Now-this.TransportationForm.StartTime.Value;
            this.TransportationForm.Duration = (decimal)duration.TotalHours;
            this.TransportationForm.UpdateBy = WebContext.Current.CurrentUser.Name;
            this.TransportationForm.UpdateDate = DateTime.Now;
            new TransportationFormBiz().Save(this.TransportationForm);
            MessageBox("入厂登记成功");
            lockControls();
        }

        protected void btnUnloaded_Click(object sender, EventArgs e)
        {
            decimal quantity = 0.0M;
            if (decimal.TryParse(this.txtUnloadQuantity.Text, out quantity))
            {
                this.TransportationForm.UnloadQuantity = quantity;
            }
            else
            {
                MessageBox("卸车地净重量输入有误！");
            }
            if (decimal.TryParse(this.txtUnloadTatleQuantity.Text, out quantity))
            {
                this.TransportationForm.UnloadTotalQuantity = quantity;
            }
            else
            {
                MessageBox("卸车地总重量输入有误！");
            }
            if (decimal.TryParse(this.txtUnloadVehicleQuantity.Text, out quantity))
            {
                this.TransportationForm.UnloadVehicleQuantity = quantity;
            }
            else
            {
                MessageBox("卸车地皮重量输入有误！");
            }

            //this.TransportationForm.UnloadConfirmUser = Guid.Parse(this.ddlUnloadConfirmUser.SelectedValue);
            //this.TransportationForm.UnloadShiftUser = WebContext.Current.CurrentUser.ID;
            this.TransportationForm.UnloadTime = DateTime.Now;

            this.TransportationForm.Status = Guid.Parse(StringConstant.StatusUnloaded);
            this.TransportationForm.UpdateBy = WebContext.Current.CurrentUser.Name;
            this.TransportationForm.UpdateDate = DateTime.Now;
            //DateTime shiftDate;
            //new Common().ShiftTypeCalculate(DateTime.Now, out shiftDate);

            //this.TransportationForm.UnloadShiftDate = WebContext.Current.CurrentUser.ShiftDate;
            //this.TransportationForm.UnloadShiftType = Guid.Parse(this.ddlUnloadShiftType.SelectedValue);

            new TransportationFormBiz().Save(this.TransportationForm);
            MessageBox("卸货登记成功");
            lockControls();
        }
    }
}